#include "GraphicsUtil.h"
#define USE_ALLEGRO_BLENDER

GraphicsUtil::GraphicsUtil(void)
{
	// push back a bunch of levels
	buffers.push_back(create_bitmap(SCREEN_W, SCREEN_H));
	buffers.push_back(create_bitmap(SCREEN_W, SCREEN_H));
	buffers.push_back(create_bitmap(SCREEN_W, SCREEN_H));
	buffers.push_back(create_bitmap(SCREEN_W, SCREEN_H));
}

GraphicsUtil::~GraphicsUtil(void)
{
	for (vector<BITMAP *>::const_iterator itr = buffers.begin(); itr < buffers.end(); ++itr)
	{
		BITMAP *bitmap = static_cast<BITMAP *>(*itr);
		clear_bitmap(bitmap);
		destroy_bitmap(bitmap);
		bitmap = NULL;
	}
	buffers.clear();
}

bool GraphicsUtil::checkLevel(const int level)
{
	if (level > buffers.size())
	{
		return false;
	}
	return true;
}

void GraphicsUtil::drawToBackBuffer(BITMAP *imageA, const int x, const int y, const int level)
{
	if (checkLevel(level))
	{
		draw_sprite(buffers[level], imageA, x, y);
	}
}

void GraphicsUtil::drawBackBufferToScreen(const int level)
{
	if (checkLevel(level))
	{
		blit(buffers[level], screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
		clear_bitmap(buffers[level]);
	}
}

void GraphicsUtil::fader(BITMAP *imageA, BITMAP *imageB, const int ratio)
{
	set_trans_blender(255, 255, 255, ratio);
	draw_trans_sprite(buffers[0], imageA, 0, 0);
	set_trans_blender(255, 255, 255, 255 - ratio);
	draw_trans_sprite(buffers[0], imageB, 0, 0);
}